Vue.prototype.base_url = '';//ip地址
import VueRouter from 'vue-router' 
import Vue from "vue" //引入vue
import app from './app.vue' //引入组件
import VueResource from 'vue-resource'//引入vue-resource
import router from './router.js' //引入router 路由
import Vuex from 'vuex' //配套的公共管理工具

import './css/index.css'
import 'bootstrap/dist/css/bootstrap.css' //引入bootstrap
import 'bootstrap/dist/js/bootstrap.min.js' //引入bootstrap
import init from './js/init.js' //引入信息配置

Vue.use(VueRouter) //安装路由y
Vue.use(VueResource) //用于请求数据交互
Vue.use(Vuex) //安装配套的公共管理工具



//引入vuexvuex是vue配套的公共管理工具，他可以把一些公共的数据，
//保存到vuex中方便整个程序中的任何组件直接获取或修改公共的资源！
var store = new Vuex.Store({
    state:{//相当于data，不建议直接修改state里面的值，不方便以后维护，做好借助与mutations里面的函数修改
        infoList:localStorage.getItem('infoList')||"", // 把消息存在缓存里
        targetId:'',
        currentId:localStorage.getItem('currentId')||'',
        infoListParse:localStorage.getItem('infoList')?(JSON.parse(localStorage.getItem('infoList'))||{}):{},
        sendInfoFun:function(){},
        callbackInfo:{},
        userInfo:null
    },
    mutations:{//相当于methods
        // 首次加载时调用
        setCurrentId(state,currentId){
            state.currentId = currentId       
            localStorage.setItem('currentId',JSON.stringify(state.currentId))
        },
        //有新信息的时候更新信息
        setInfoList(state,infoList){//mutations里面的函数第一个参数值是固定的state
            if(state.infoList != "{}"){
                for(var item in state.infoListParse){
                    if(state.infoListParse[infoList.targetId]){
                        if(item == infoList.targetId){//判断对话是否存在存在时就找到对应的targetId  push进去
                            state.infoListParse[item].push(infoList)
                        }
                    }else{
                        state.infoListParse[infoList.targetId] = [infoList]
                    }
                }
            }else{
                state.infoListParse[infoList.targetId] = [infoList]
            }
            state.infoList = state.infoListParse
            localStorage.setItem('infoList',JSON.stringify(state.infoListParse))
        },
        //切换不同的人时更新targetId
        setTargetId(state,datas){
            state.userInfo = datas
            state.targetId = datas.id   
            localStorage.setItem('targetId',JSON.stringify(state.targetId))
        },
        //获取公共信息
        getState(state,msg){
            return state[msg]
        },
        sendInfo(state,arg){
            state.sendInfoFun(arg)
        }

    },
    getters:{//和computed和filter比较像，getters不负责修改数据负责向外提供数据想要修改数据去mutations
        // setNum :function(state){
        //     // console.log(state.car[0].num)
        //     var c = 0
        //     state.car.forEach(function(e){
        //         c += e.num
        //     })
        //     return c
        // }
    }
})
Vue.filter("time",function(msg,val){
    if(msg){
        msg = typeof msg=="string"?msg.indexOf('T')?msg:msg.split("-").join('/'):msg
        var t = new Date(msg)
        var o = new Date()
        var y = t.getFullYear()
        var m = (t.getMonth()+1)>9?(t.getMonth()+1):("0"+(t.getMonth()+1))
        var d = t.getDate()>9?t.getDate():("0"+t.getDate())
        var h = t.getHours()>9?t.getHours():("0"+t.getHours())
        var min = t.getMinutes()>9?t.getMinutes():("0"+t.getMinutes())
        var s = t.getSeconds()>9?t.getSeconds():("0"+t.getSeconds())
        if(val == 'yyyy-mm-dd'){
            return y +"-"+ m + "-" + d 
        }else if(val == 'h-min-s'){
            debugger
            if(msg>Date.parse(o.getFullYear() +"-"+ (o.getMonth()+1) + "-" + o.getDate() + " 00:00:00")){
                return h +":"+ min + ":" +s
            }
            return y +"-"+ m + "-" + d +" "+ h +":"+ min 
        }else{
           return y +"-"+ m + "-" + d +" "+ h +":"+ min + ":" +s
        }
    }
})

 var vm = new Vue({
     el:"#app",
     data:{
        init:init, //聊天初始化
        targetId:null,//目标id
        currentId:null //当前登陆的id
     },
     render:c=>c(app),
     router,
     store,
     created(){
        var that = this
        // 用户名是小牛
        // this.targetId = "token_1"
        // var params = {
        //     appKey : "4z3hlwrv4o3dt",
        //     token : "F0YocXTIX0RBPHKLz35NBokEJCrTxAc6Wb6wm+zf6BKcmqAWuqYQ9me2VqNbb9eVcJbDaYVzC6Y3fcvKfi+eVQ=="
        // };

        // this.targetId = "token"
        // // store.commit("setTargetId",this.targetId)
        //     // 用户名是小牛_1向小牛_2发消息
        //  var params = {
        //      appKey : "4z3hlwrv4o3dt",
        //      token : "hVrmz+3BCN8xd5DS4tQwVYkEJCrTxAc6Wb6wm+zf6BKcmqAWuqYQ9on3x8zvYo3SapOBTtbAd8fmDt40OiFXRDEQNRMdgSmI"
        //  };


        //  用户名是小牛_2向小牛_1发消息
        //  this.targetId = "token_1"
         var params = {
            appKey : "4z3hlwrv4o3dt",
            token : "8wO/ECW2z09UsOxrOk9dIYkEJCrTxAc6Wb6wm+zf6BKcmqAWuqYQ9n4QSpIg55lo5JMFf7S2mxvQbDym1o+RZTEQNRMdgSmI"
        };
         var userId = "";
         var callbacks = {
             getInstance : function(instance){
             },
             getCurrentUser : function(userInfo){ //首次初始化
                 that.targetId = userInfo.userId
                 store.commit("setCurrentId",userInfo.userId)
             },
             receiveNewMessage : function(message){
                store.commit("setInfoList",message)
                 // show(message);
             },
             sendInfo:function(fun){//把发送信息的方法添加到公共文件上
                store.state.sendInfoFun = fun
             },
             sendInfoCallback:function(callbackInfo){
                store.state.callbackInfo = callbackInfo
             }
         };
         this.init(params,callbacks);
     },
     methods:{

     }
 })